package me.goldze.mvvmhabit.binding.viewadapter.edittext

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.text.method.MovementMethod
import android.text.method.TransformationMethod
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.qmuiteam.qmui.util.QMUIKeyboardHelper
import me.goldze.mvvmhabit.binding.command.BindingCommand

/**
 * Created by goldze on 2017/6/16.
 */

object ViewAdapter {
    /**
     * EditText重新获取焦点的事件绑定
     */
    @BindingAdapter(value = ["requestFocus"], requireAll = false)
    @JvmStatic
    fun requestFocusCommand(editText: EditText, needRequestFocus: Boolean) {
        if (needRequestFocus) {
            editText.setSelection(editText.text.length)
            editText.requestFocus()
            val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
        }
        editText.isFocusableInTouchMode = needRequestFocus
    }

    @BindingAdapter(value = ["readonly"])
    @JvmStatic
    fun setReadOnly(editText: EditText, readOnly: Boolean = false) {
        editText.isCursorVisible = !readOnly         //设置输入框中的光标不可见
        editText.isFocusable = !readOnly             //无焦点
        editText.isFocusableInTouchMode = !readOnly  //触摸时也得不到焦点
    }

    /**
     * EditText输入文字改变的监听
     */
    @BindingAdapter(value = ["textChanged"], requireAll = false)
    @JvmStatic
    fun addTextChangedListener(editText: EditText, textChanged: BindingCommand<String>?) {
        editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

            }

            override fun onTextChanged(text: CharSequence, i: Int, i1: Int, i2: Int) {
                textChanged?.execute(text.toString())
            }

            override fun afterTextChanged(editable: Editable) {

            }
        })
    }

    @BindingAdapter(value = ["onSearchCommand"])
    @JvmStatic
    fun onSearchListener(editText: EditText, onSearchListener: BindingCommand<Context>?) {
        editText.setOnEditorActionListener { v: TextView?, actionId: Int?, _: KeyEvent? ->
            v?.let { v ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_SEND) {
                    QMUIKeyboardHelper.hideKeyboard(editText)
                    onSearchListener?.execute(v.context)
                    return@setOnEditorActionListener false
                }
            }
            return@setOnEditorActionListener true;
        }
    }

    @BindingAdapter(value = ["transformationMethod"])
    @JvmStatic
    fun setTransformationMethod(editText: EditText, method: TransformationMethod) {
        editText.transformationMethod = method
    }
    @BindingAdapter(value = ["movementMethod"])
    @JvmStatic
    fun setMovementMethod(editText: EditText, method: MovementMethod) {
        editText.movementMethod = method
    }

}